package com.bjqh.edu.UI.Activity.Index;

import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.bjqh.edu.Base.AppConstants;
import com.bjqh.edu.Bean.Order;
import com.bjqh.edu.R;
import com.bjqh.edu.UI.Activity.StudentPersonal.PaymentActivity;
import com.bjqh.edu.UI.Activity.StudentPersonal.PendingDetailActivity;
import com.bjqh.edu.UI.Utils.ComnUtils;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.HttpParams;

import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Response;

import static com.bjqh.edu.Base.AppConstants.H5_URL;


/**
 * h5 页面容器
 */
public class WebViewAcitviy extends AppCompatActivity {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.webview)
    WebView webView;
    @BindView(R.id.progressBar)
    ProgressBar pg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.web_view);
        ButterKnife.bind(this);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        String url = getIntent().getStringExtra("url");
        if (TextUtils.isEmpty(url)) {
            ComnUtils.getInstanceOfComnUtils(WebViewAcitviy.this).Tip("暂无加载的内容");
            finish();
            return;
        }

        if (url.startsWith("http://") || url.startsWith("https://")) {
            webView.loadUrl(url);
        } else {
            webView.loadUrl(H5_URL + url);
        }

        webView.setWebChromeClient(new WebChromeClient() {

            @Override
            public void onReceivedTitle(WebView view, String titleValue) {
                if (TextUtils.isEmpty(titleValue)) {
                    tvTitle.setText("千禾学堂");
                } else {
                    tvTitle.setText(titleValue);
                }
                super.onReceivedTitle(view, titleValue);
            }

            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                // TODO 自动生成的方法存根

                if (newProgress == 100) {
                    webView.setVisibility(View.VISIBLE);
                    pg.setVisibility(View.GONE);//加载完网页进度条消失
                } else {
                    pg.setVisibility(View.VISIBLE);//开始加载网页时显示进度条
                    pg.setProgress(newProgress);//设置进度值
                }

            }
        });
        webView.setWebViewClient(new WebViewClient() {
            //设置在webView点击打开的新网页在当前界面显示,而不跳转到新的浏览器中
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                webView.setVisibility(View.GONE);
                if (url.startsWith("tel:")) {
                    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
                    return true;
                }
                if (url.startsWith("sms:")) {
                    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
                    return true;

                }
                if (TextUtils.equals(url, "data:text/plain,")) {
                    return false;
                }
                if (url.startsWith("http://qianhexuetangpay")) {
                    if (Uri.parse(url).getQueryParameter("qianhexuetang") != null && Uri.parse(url).getQueryParameter("qianhexuetang").equals("qianhexuetangpay")) {
                        String goodtitle = Uri.parse(url).getQueryParameter("goodname");
                        String price = Uri.parse(url).getQueryParameter("price");
                        String goodname = Uri.parse(url).getQueryParameter("goodtitle");
                        String goodid = Uri.parse(url).getQueryParameter("goodid");
                        if (TextUtils.isEmpty(goodtitle)) {
                            ComnUtils.getInstanceOfComnUtils(WebViewAcitviy.this).Tip("系统出错");
                            return false;
                        }
                        if (TextUtils.isEmpty(goodname)) {
                            ComnUtils.getInstanceOfComnUtils(WebViewAcitviy.this).Tip("系统出错");
                            return false;

                        }


                        if (TextUtils.isEmpty(price)) {
                            ComnUtils.getInstanceOfComnUtils(WebViewAcitviy.this).Tip("系统出错");
                            return false;
                        }

                        if (TextUtils.isEmpty(goodid)) {
                            ComnUtils.getInstanceOfComnUtils(WebViewAcitviy.this).Tip("系统出错");
                            return false;

                        }
                        Intent intent = new Intent();
                        intent.putExtra("price", Uri.parse(url).getQueryParameter("price"));
                        intent.putExtra("coursedes", Uri.parse(url).getQueryParameter("goodtitle"));
                        intent.putExtra("coursename", Uri.parse(url).getQueryParameter("goodname"));
                        intent.putExtra("cid", Uri.parse(url).getQueryParameter("goodid"));
                        intent.setClass(WebViewAcitviy.this, PaymentActivity.class);
                        startActivity(intent);

                    }
                    return false;

                }


                if (url.startsWith("http://") || url.startsWith("https://")) {
                    view.loadUrl(url);
                    return false;
                }


                view.loadUrl(H5_URL + url);
                return false;
            }

            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                super.onReceivedError(view, request, error);
            }
        });
        WebSettings webSettings = webView.getSettings();
        //①设置WebView允许调用js
        webSettings.setJavaScriptEnabled(true);
        webSettings.setDefaultTextEncodingName("UTF-8");
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webSettings.setUseWideViewPort(true);//设定支持viewport
        webSettings.setLoadWithOverviewMode(true);   //自适应屏幕
        webSettings.setBuiltInZoomControls(true);
        webSettings.setDisplayZoomControls(false);
        webSettings.setSupportZoom(true);//设定支持缩放

        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (webView.canGoBack()) {
                    webView.goBack();
                } else {
                    finish();
                }

            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()) {
            webView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

}
